﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MSXML2;
using System.Xml;
using System.Text.RegularExpressions;
public partial class Dom操作_XMLHTTP_扣17173的东东 : System.Web.UI.Page
{
    /// <summary>
    /// 最下面分页的代码
    /// </summary>
    string footList = "";
    /// <summary>
    /// 得到临时的 分页的代码 数据 
    /// </summary>
    string foottemp = "";
    string str; 
    string strxx="";
    int num=0;//得到DIV的总数
    TongUse1 TextData = new TongUse1();
    protected void Page_Load(object sender, EventArgs e)
    {

        Page.Title = "sdfsdfsd";


        XMLHTTPClass xmlhttp = new XMLHTTPClass();
        xmlhttp.open("get", "http://search.17173.com/bk.jsp?keyword=1&submit=", false, "", "");
        xmlhttp.send(null);
        if (xmlhttp.readyState == 4)
        {
            cont.Text = XmlTextData(xmlhttp.responseText);
            str = cont.Text.ToLower();//变小写
            str = xhtml(str);//格式化 html 看看能不能  XML
            Regex r = new Regex(@"<div[^>]\s{0,}class\s{0,}=\s{0,}[""""']\s{0,}searchpage+\s{0,}[""""']\s{0,}>[\w\W]*<\s{0,}/\s{0,}a{0,}>\s{0,}<\s{0,}/\s{0,}div\s{0,}>");
            strxx = r.Match(xhtml(str)).Value; //得到分页的那东东的内容

            if (strxx != "")
            {
                //替换掉 分页东东里的　A 
                //回车和换行要换掉
                MatchCollection regString = Regex.Matches(strxx.Replace("'", "\"").Replace("</a>", "</a>\n"), @"<\s{0,}a\s{0,}[^>$ ].+</\s{0,}a\s{0,}>");
                for (int l = 0; l < regString.Count; l++)
                {
                    foottemp += regString[l].Value.Replace("\r", "").Replace("\n", "");
                    footList += regString[l].Value.Replace("href=", "href=\"").Replace(">", "\">").Replace("a\"", "a").Replace("\r", "").Replace("\n", "");
                }

                //得到转化后的 分页 数据
               // Response.Write("111" + strxx.Replace("\r", "").Replace("\n", "").Replace(foottemp, footList));

                //把数据换成特别的
                str = xhtml(str).Replace(strxx, strxx.Replace("\r", "").Replace("\n", "").Replace(foottemp, footList));
            }
            else
            {
                str = xhtml(str);
            }
        }
        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
        xml.LoadXml("<Data>"+str+"</Data>");

        num = xml.DocumentElement.GetElementsByTagName("div").Count;
        for (int i = 0; i < num; i++)
        {
            try
            {
                if (xml.DocumentElement.GetElementsByTagName("div").Item(i).Attributes.GetNamedItem("id").Value == "wrapper")
                {
                    strxx = "";
                    strxx += "<ul>";
                    strxx += xml.DocumentElement.GetElementsByTagName("div").Item(i).InnerXml;
                    strxx += "</ul>";

                }
            }
            catch
            {

            }
        }
        cont.Text = strxx;
        cont.Text = cont.Text.Replace("&amp;nbsp;", "&nbsp;");
        cont.Text = Regex.Replace(cont.Text, @"<[^>]+>.*/\w{6}.?/ig", "");//替换掉所有脚本
        cont.Text = Regex.Replace(cont.Text, @"提问者：<[^>]+>.*\s{0,}\w{3}n>.*", "");
        cont.Text = Regex.Replace(cont.Text, @"回答者：<[^>]+>.*\s{0,}\w{3}n>.*", "");
        cont.Text = Regex.Replace(cont.Text, @"apparise.*对您的评价", "apparise\">评价");//替换 'apparise">lixiangccdi 对您的评价'
        cont.Text = Regex.Replace(cont.Text, @"apparise.*星级: </\s{0,}\w{3}>", "\"/>");//替换 'apparise">lixiangccdi 对您的评价：xiexie  <br />评定星级: </div>
        cont.Text = Regex.Replace(cont.Text, @"\$.*\$", "");//替换掉所有 以$ 开头，以＄结速的符


        /////////////////////////////////////



        



    }


    protected String XmlTextData(string strXdata)
    {
        strXdata = strXdata.Replace("&", "&#38;");

        //替换 原始文档
        strXdata = strXdata.Replace("http://search.17173.com/bk.jsp", Request.RawUrl);

        strXdata = strXdata.Replace("http://bk.17173.com/", "http://"+Request.Url.Host+"/");

        //替换 原始文档
       // strXdata = strXdata.Replace("bk.jsp", Request.RawUrl);

        //替换 标题
        strXdata = strXdata.Replace("/ask/", "game/baike/showdoc.aspx?url=/ask/");

        //替换 \"
        strXdata = strXdata.Replace("\\\"", "");
        //替换\'
        strXdata = strXdata.Replace("\\\'", "");
        //替换 '
        strXdata = strXdata.Replace("\'", "");
        //替换 17173 的bad数据
        //  xmlcont = xmlcont.Replace("<div class=\"noaskDiv\">","<a>");

        strXdata = strXdata.Replace("<td>提问者</td><td id=\"managetd\">管理</td>", "");
        //替换 <span class="font12">

        // xmlcont = xmlcont.Replace("<span class=\"font12\">", "");
        // xmlcont = xmlcont.Replace("<span class=\"question_table_icon\">", "");
        // xmlcont = xmlcont.Replace("</span>","");
        // xmlcont = xmlcont.Replace("<script>setAskTotal('55');</script>", "");
        ///xmlcont = xmlcont.Replace("=", "&#61;"); 
        // res.Response.Write(xmlcont);
        return strXdata;
    }

    /// <summary>
    /// html
    /// </summary>
    /// <param name="html"></param>
    /// <returns></returns>
    public String xhtml(string html)
    {
        html = html.ToLower();
        html = Regex.Replace(html, "<meta .*content-type.*>","");//替换  content-type
       // html = Regex.Replace(html, @"<![^>]+>", "");
        html = Regex.Replace(html, @"<link[^>]+>", "");
        html = Regex.Replace(html, @"<sc[^>]+>|</sc[^>]+>", "");
        html = Regex.Replace(html, @"<ht[^>]+>", "<html>");
        html = Regex.Replace(html, @"<img[^>]+>", "");//把ＩＭＧ搞米有
        html = Regex.Replace(html, @"<fo[^>]+>|</fo[^>]+>", "");//替换ＦＯＲＭ

        return html;
    }
}
